package com.example.lenovo.mywechat;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * create by cw 2018/11/4  16:35
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List<Msg> msgList=new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView ;
    private MsgAdapter adapter;

    //声明四个ImageButton对象
    private ImageButton btn_weChat;
    private ImageButton btn_contact;
    private ImageButton btn_find;
    private ImageButton btn_me;
    //声明fragment对象
    //WeChatFragment weChatFragment=null;
    ContactFragment contactFragment=null;
    FindFragment findFragment=null;
    MeFragment meFragment=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMsg();//初始化消息数据
        inputText=findViewById(R.id.input_text);
        send= findViewById(R.id.send);
        msgRecyclerView=findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(this);
        //通过id找到对应的按钮
        btn_weChat=findViewById(R.id.btn_wechat);
        btn_contact=findViewById(R.id.btn_contact);
        btn_find=findViewById(R.id.btn_find);
        btn_me=findViewById(R.id.btn_me);
        //按钮添加监听事件
        btn_weChat.setOnClickListener(this);
        btn_contact.setOnClickListener(this);
        btn_find.setOnClickListener(this);
        btn_me.setOnClickListener(this);

        selectIndex(0);
    }

    private void initMsg() {
        Msg msg1=new Msg("hello guy.",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2=new Msg("hello,who is that",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3=new Msg("This is Tom.Nice to meet you.",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    @Override
    public void onClick(View v) {
        String content=inputText.getText().toString();
        if(!"".equals(content)){
            Msg msg=new Msg(content,Msg.TYPE_SENT);
            msgList.add(msg);
            adapter.notifyItemInserted(msgList.size()-1);
            msgRecyclerView.scrollToPosition(msgList.size()-1);
            inputText.setText("");
        }
        switch (v.getId()){
            case R.id.btn_wechat:
                selectIndex(0);
                break;
            case R.id.btn_contact:
                selectIndex(1);
                break;
            case R.id.btn_find:      
                selectIndex(2);
                break;
            case R.id.btn_me:
                selectIndex(3);
                break;
                 default:
                     Log.e("Maintivity","123");
        }
    }
    /**
     * 根据传入的index参数来设置选中的tab页。
     */
    private void selectIndex(int i) {
        //重置按钮
        btnReSet();
        // 开启一个Fragment事务
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragment(transaction);
        switch (i){
            case 0:
                btn_weChat.setImageResource(R.mipmap.tab_weixin_pressed);
               /* if(weChatFragment==null){
                    weChatFragment=new WeChatFragment();
                    transaction.add(R.id.fl_content,weChatFragment);
                }else{
                    transaction.show(weChatFragment);
                }
                */
                break;
            case 1:
                btn_contact.setImageResource(R.mipmap.tab_contact_pressed);
                if(contactFragment==null){
                    contactFragment=new ContactFragment();
                    transaction.add(R.id.fl_content,contactFragment);
                }else{
                    transaction.show(contactFragment);
                }
                break;
            case 2:
                btn_find.setImageResource(R.mipmap.tab_find_pressed);
                if(findFragment==null){
                    findFragment=new FindFragment();
                    transaction.add(R.id.fl_content,findFragment);
                }else{
                    transaction.show(findFragment);
                }
                break;
            case 3:
                btn_me.setImageResource(R.mipmap.tab_setting_pressed);
                if(meFragment==null){
                    meFragment=new MeFragment();
                    transaction.add(R.id.fl_content,meFragment);
                }else{
                    transaction.show(meFragment);
                }
                break;
                default:
                    break;
        }
        //提交事务
        transaction.commit();
    }
    /**
     * 将所有的Fragment都置为隐藏状态。
     * 用于对Fragment执行操作的事务
     */
    private void hideFragment(FragmentTransaction transaction) {
       /* if(weChatFragment!=null){
            transaction.hide(weChatFragment);
        }
        */
        if(contactFragment!=null){
            transaction.hide(contactFragment);
        }
        if(findFragment!=null){
            transaction.hide(findFragment);
        }
        if(meFragment!=null){
            transaction.hide(meFragment);
        }
    }
    // 重置按钮
    private void btnReSet() {
        btn_weChat.setImageResource(R.mipmap.tab_weixin_normal);
        btn_contact.setImageResource(R.mipmap.tab_contact_normal);
        btn_find.setImageResource(R.mipmap.tab_find_normal);
        btn_me.setImageResource(R.mipmap.tab_setting_normal);
    }
}
